package com.cinyi.cyds.base.mongo;

import java.sql.Timestamp;
import java.util.Date;

import sun.tools.jar.resources.jar;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cinyi.cyds.util.StringUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MapReduceCommand;

/**
 * mongo.group(key, cond, initial, reduce,finalize)<br>
 * {"db":"message","key":"topic,fromName","cond":[{"item":"count,$gte,int:0"}],"init":"count:0,total:0","reduce":"aa","finalize":"bb"}
 * @author penghui
 *
 */
public class MongoMapCommand implements java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 6522618755924763745L;
	public static final String TAG_DB_NAME = "db" ;
	public static final String TAG_KEY = "key" ;
	public static final String TAG_REDUCE = "reduce" ;
	public static final String TAG_FINALIZE= "finalize" ;
	public static final String TAG_QUERY = "query" ;
	
	private String dbName  = null ;
	private String emit = null ;
	private String reduce  = null ;
	private String finalize  = null ;
	private String query  = null ;
	private MapReduceCommand.OutputType outputType ;
	
	private DBObject queryDBobject = null ;
	
	public MongoMapCommand(){}
	
	public MongoMapCommand(String jsonStr){
		JSONObject json = JSONObject.parseObject(jsonStr);
		dbName = json.getString(TAG_DB_NAME);
		emit = json.getString(TAG_KEY);
		reduce = json.getString(TAG_REDUCE);
		finalize = json.getString(TAG_FINALIZE);
		query = json.getString(TAG_QUERY);
		buildQuery();
		
	}
	
	private void buildQuery() {
		JSONArray jarray = JSONObject.parseArray(query);
		if(jarray==null||jarray.size()==0){
			return ;
		}
		queryDBobject = new BasicDBObject();
		int size = jarray.size();
		for(int i=0;i<size;i++){
			JSONObject ajson = jarray.getJSONObject(i);
			String[] cond = ajson.getString("item").split(",");
			int pos = cond[2].lastIndexOf(":"); //type:val
			String[] vals = new String[2];
			vals[0]=cond[2].substring(0,pos);
			vals[1]=cond[2].substring(pos+1,cond[2].length());
			if(int.class.getSimpleName().equalsIgnoreCase(vals[0]) || 
					Integer.class.getSimpleName().equalsIgnoreCase(vals[0]) || 
					Long.class.getSimpleName().equalsIgnoreCase(vals[0]) ){
				queryDBobject.put(cond[0], new BasicDBObject(cond[1], StringUtil.getLong(vals[1], 0)));
			}else if(Date.class.getSimpleName().equalsIgnoreCase(vals[0]) || 
					Timestamp.class.getSimpleName().equalsIgnoreCase(vals[0]) ){
				queryDBobject.put(cond[0], new BasicDBObject(cond[1], StringUtil.getLong(vals[1], 0)));
			}else{
				if(vals[1]==null || "null".equalsIgnoreCase(vals[1])){
					queryDBobject.put(cond[0], new BasicDBObject(cond[1], null));
				}else{
					queryDBobject.put(cond[0], new BasicDBObject(cond[1], vals[1]));
				}
				
			}
			
		}
	
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public String getEmit() {
		return emit;
	}

	public void setEmit(String emit) {
		this.emit = emit;
	}

	public String getReduce() {
		return reduce;
	}

	public void setReduce(String reduce) {
		this.reduce = reduce;
	}

	public MapReduceCommand.OutputType getOutputType() {
		return outputType;
	}

	public void setOutputType(MapReduceCommand.OutputType outputType) {
		this.outputType = outputType;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public String getFinalize() {
		return finalize;
	}

	public void setFinalize(String finalize) {
		this.finalize = finalize;
	}

	public DBObject getQueryDBobject() {
		return queryDBobject;
	}

}
